﻿/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/

////获得章节--2.0没有这个功能
//function InitCatalog() {

//    jQuery.ajax({
//        type: "Get",
//        async: false,
//        url: "/Ebook/GetChapterAndPart/" + jQuery("#txt_EbookKey").val(),
//        success: function (data) {

//            jQuery("#chapterSelect").empty();

//            jQuery("#chapterSelect").append('<option value="0">--请选择--</option>');

//            for (var i = 0; i < data.length; i++) {

//                jQuery("#chapterSelect").append('<option value=' + data[i].Key + '>' + data[i].Name + '</option>');
//            }
//        }
//    });
//};

KindEditor.plugin('anchorcware', function (K) {

    var self = this,
        ebookId = jQuery("#EbookId").val(),
        name = 'anchorcware';

    self.clickToolbar(name, function () {

        //var chapter = InitCatalog();
        //self.focus()  产生的BUG是弹出两个窗体
        //self.focus();
        //页面布局
        var html = [
                    '<div  style="padding:20px;overflow-x : auto; overflow-y: auto; height : 400px;">',
                    '<table cellpadding="0" cellspacing="0" style="line-height:30px; border:1px;" width="100%" class="table">',

                    '<tr>',
                    '<td align="right" valign="top" style="width:100px;">课件标题：</td>',
                    '<td align="left"><span id="txtCoursewareTitle"></span>&nbsp;<input type="button" id="btnBrowserCourseware" value="浏览课件"></td>',
                    '</tr>',

                    //2.0不需要文件名称，注掉
                    //'<tr>',
                    //'<td align="right">文件名称：</td>',
                    //'<td align="left"><span  id="txtCoursewareFileName"></span></td>',
                    //'</tr>',

                    '<tr>',
                    '<td align="right" valign="top">缩略图：</td>',
                    '<td align="left"><img id="imgCoursewareCover" alt="" width="100" height="100"></td>',
                    '</tr>',

                    '<tr>',
                    '<td align="right" valign="top">课件说明：</td>',
                    '<td align="left"><span id="txtCoursewareDescription"></span></td>',
                    '</tr>',

                    ////关联知识点暂时先不做
                    //'<tr>',
                    //'<td align="right" valign="top">关联知识点：</td>',
                    //'<td align="left"><span id="keyPointName"></span></td>',
                    //'</tr>',
                    //课件内/外
                    '<tr>',
                    '<td align="right" valign="top">课件内/外：</td>',
                    '<td align="left">' +
                    '<select id="cwInOut">' +
                    '<option value="in">课件内</option>' +
                    '<option value="out">课件外</option>' +
                    '</select>' +
                    '</td>',
                    '</tr>',

                    '<tr>',
                    '<td align="right" valign="top">宽：</td>',
                    '<td align="left"><input id="txtWidth" type="text" width="50px" value="280" /></td>',
                    '</tr>',

                    '<tr>',
                    '<td align="right" valign="top">高：</td>',
                    '<td align="left"><input id="txtHeight" type="text" width="50px" value="210" /></td>',
                    '</tr>',

                    '<tr><td><input id="txtCoursewareKey" type="hidden" /><input id="txtVideoType" type="hidden" /><input id="txtFileURL" type="hidden" /><input type="hidden" id="txtCoursewareType"/>',
                    '</td></tr>',
                    '</table>',
                '</div>'].join('');

        //页面弹框
        var dialog = self.createDialog({

            name: "插入课件",
            width: 650,
            height: 500,
            title: "插入课件",
            body: html,
            yesBtn: {//确认插入按钮（插入到正文的富文本编辑器）
                name: self.lang('yes'),
                click: function (e) {

                    if (jQuery("#txtCoursewareTitle").html() == "") {

                        alert("请选择课件");

                        return false;
                    }

                    var key = jQuery("#txtCoursewareKey").val(),
                        action = "courseware_",
                        //这个是课件标题
                        txt = jQuery("#txtCoursewareTitle").html(),
                        coursewareType = jQuery("#txtCoursewareType").val(),
                        coverSrc = jQuery("#imgCoursewareCover").attr("src"),
                        videoType = jQuery("#txtVideoType").val(),
                        fileURL = jQuery("#txtFileURL").val(),
                        img = jQuery("#imgCoursewareCover"),
                        width = jQuery("#txtWidth").val(),
                        height = jQuery("#txtHeight").val(),
                        cwInOut = jQuery("#cwInOut").val();

                    //if (!/^\d*jQuery/.test(width) || width == "") {
                    //    width = 280;
                    //}
                    //if (!/^\d*jQuery/.test(height) || height == "") {
                    //    height = 210;
                    //} 
                    width = (width.trim() == "" || width <= 0) ? 280 : width;
                    height = (height.trim() == "" || height <= 0) ? 210 : height;
                    //by luodaishi 20141127(由书内预览变为书外预览)
                    //var str = '<courseware id="' + key + '" cwtype="' + coursewareType + '" cwurl="/CoursewareEditManage/Coursewareshow?coursewareID=' + key + '&cwTypeID=' + coursewareType + '"><img src="' + src + '"></img></courseware>';
                    //http://192.168.0.15:10015 该地址是编辑端的地址，以后会剔除，现在的原因是课件开发人员时间赶不过来
                    //var str = '<div style=" margin: 0em auto; width: 100%;">' +
                    //    '<iframe class="courseware" style="width:100%;border:0px none;overflow:hidden;height:700px;" src="http://192.168.0.15:10015/CoursewareEditManage/Coursewareshow?coursewareID=' + key + '&cwTypeID=' + coursewareType + '">' +
                    //    '</iframe>' +
                    //    '</div>';
                    var str = '<courseware' +
                        ' id="' + key + '"' +
                        ' name="' + key + '"' +
                        ' cwurl="/FileProgress/CoursewarePlay/' + key + '"' +
                        ' cwex="/FileProgress/CoursewareExplain/' + key + '"' +
                        ' cwtype="' + coursewareType + '"' +
                        ' cwshowmodel="' + cwInOut + '">' +
                        '<img' +
                        ' id="imgCoursewareCover"' +
                        ' alt="" width="' + width + '"' +
                        ' height="' + height + '"' +
                        ' src="' + coverSrc + '" />' +
                        '</courseware>';
                    self.insertHtml(str).hideDialog().focus();
                    ////2.0不需要这么多格式，直接打包了，所以switch-case注掉
                    //switch (videoType) {

                    //    case "video":
                    //        var html = [

                    //        '<video controls="" poster="' + src + '" height="' + height + '" width="' + width + '" preload="none" class="video-js vjs-default-skin coursewareanchor" target="' + key + '" tabindex="0">',
                    //        '<source type="video/webm" src="/FileProcess/DownLoad/' + fileURL + '?type=\'webm\'"></source>',
                    //        '<source type="video/mp4" src="/FileProcess/DownLoad/' + fileURL + '?type=\'mp4\'"></source>',
                    //        '</video>'
                    //        ].join('');

                    //        self.insertHtml(html).hideDialog().focus();
                    //        break;

                    //    case "mp3":

                    //        var html = [

                    //            '<audio target="' + key + '" class="coursewareanchor" src="/FileProcess/DownLoad/' + fileURL + '" controls="controls">Your browser does not support the audio element.</audio>'

                    //        ].join('');

                    //        self.insertHtml(html).hideDialog().focus();

                    //        break;

                    //    case "flash":

                    //        var html = [

                    //            '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" target="' + key + '" class="coursewareanchor" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" title="' + txt + '" height="' + height + '" width="' + width + '">',
                    //            '<param name="movie" value="/FileProcess/DownLoad/' + fileURL + '" />',
                    //            '<param name="quality" value="high" />',
                    //            '<embed src="/FileProcess/DownLoad/' + fileURL + '" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" height="' + height + '" width="' + width + '" />',
                    //            '</object>',

                    //        ].join('');

                    //        self.insertHtml(html).hideDialog().focus();
                    //        break;
                    //        //2014.03.05 luodaishi  图片也作为课件显示
                    //    case "picture":
                    //        self.insertHtml('<img class="coursewareanchor" data-ke-src="/FileProcess/DownLoad/' + fileURL + '" src ="/FileProcess/DownLoad/' + fileURL + '"  title="' + txt + '" target=' + key + ' name=' + (action + key) + '>').hideDialog().focus();
                    //        break;
                    //        /*课件容器*/
                    //    case "ContainerGradient":
                    //    case "ContainerManyPic":
                    //    case "ContainerOnePic":
                    //    case "ContainerVideo":
                    //    case "ContainerNavigation":
                    //    case "ContainerChemistry":
                    //        //CoursewareType用于标记为课件容器
                    //        self.insertHtml('<img coursewareType="container"  class="coursewareanchor" style="cursor:pointer"  data-ke-src="' + src + '" src ="' + src + '"  title="' + txt + '" target=' + key + ' name=' + (action + key) + '>').hideDialog().focus();
                    //        break;
                    //    default:
                    //        ////Modified By：赵威 2014/09/25 如果正文页面样式出问题，再包这个a标签
                    //        //self.insertHtml('<a href="javascript:void(0)" class="coursewareanchor" title="课件锚点：' + txt + '"  target="' + key + '" name="' + action + key + '"></a>').hideDialog().focus();
                    //        self.insertHtml('<courseware id="' + key + '" cwtype="' + coursewareType + '" cwurl="/CoursewareEditManage/Coursewareshow?coursewareID=' + key + '&cwTypeID=' + coursewareType + '><img src=' + src + '></img></courseware>').hideDialog().focus();
                    //        break;
                    //}
                }
            }
        });

        //打开选择课件的页面
        jQuery("#btnBrowserCourseware").bind("click", function () {

            var dialog = K.dialog({

                width: 650,
                height: 600,
                title: '选择课件',
                body: '<div><iframe frameborder="0" id="selectCourseware" src="/EbookEdit/GetCoursewareList?ebookId=' + ebookId + '" style="height: 550px; width: 100%;"></iframe></div>',
                closeBtn: {
                    name: '关闭',
                    click: function (e) {
                        dialog.remove();
                    }
                },
                yesBtn: {
                    name: '确定',
                    click: function (e) {

                        var courseware = jQuery("#selectCourseware").contents(),
                            key = jQuery(courseware).find("#coursewareKey").val(),
                            title = jQuery(courseware).find("#coursewareTitle").val(),
                            fileName = jQuery(courseware).find("#coursewareFileName").html(),
                            cover = jQuery(courseware).find("#coursewareCourseCover").val(),
                            description = jQuery(courseware).find("#coursewareDescription").val(),
                            isVideo = jQuery(courseware).find("#coursewareVideoType").html(),
                            coursewareType = jQuery(courseware).find("#coursewareType").val(),
                            fileUrl = jQuery(courseware).find("#coursewareFileURL").html();
                        var pointName = jQuery(courseware).find("#coursewareKeyPointName").html();

                        if (key == "") {
                            alert('请选择课件');
                            return false;
                        }
                        if (title == "") {

                            alert("课件标题不能为空");
                            return false;
                        }

                        ////文件名称注掉，不要了
                        //jQuery("#txtCoursewareFileName").html(fileName);
                        //Modified By:赵威 2014/09/24 修改路径
                        jQuery("#imgCoursewareCover").attr("src", cover);
                        jQuery("#txtCoursewareTitle").html(title);
                        jQuery("#txtCoursewareDescription").html(description);
                        jQuery("#txtCoursewareKey").val(key);
                        ////知识点下一步做
                        //jQuery("#keyPointName").html(pointName);
                        jQuery("#txtVideoType").val(isVideo);
                        jQuery("#txtFileURL").val(fileUrl);
                        jQuery("#txtCoursewareType").val(coursewareType);
                        //如果是视频提示用户添加缩略图
                        if (isVideo == "video") {
                            if (cover.trim() == "") {
                                alert('视频类型需要插入缩略图');
                                return false;
                            }
                        }
                        dialog.remove();
                    }
                },
                noBtn: {
                    name: '关闭',
                    click: function (e) {

                        dialog.remove();
                    }
                }
            });
        });
    });
});

